package it.polimi.authentication.web.beans;

import java.util.Date;
import java.sql.Time;
import java.util.Calendar;

import it.polimi.authentication.ejb.usermanagement.AddNewMezzo;
import it.polimi.authentication.ejb.usermanagement.dto.MezzoDTO;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;



@ManagedBean (name="NewM")
@RequestScoped
public class NewMezzoBean {
	
	@EJB
	private AddNewMezzo ANM;
	
	
	private MezzoDTO Mezzo;
	private int OraP;
	private int minP;
	private int OraA;
	private int minA;
	
	
	public int getMinP() {
		return minP;
	}

	public void setMinP(int minP) {
		this.minP = minP;
	}

	public int getMinA() {
		return minA;
	}

	public void setMinA(int minA) {
		this.minA = minA;
	}

	public NewMezzoBean()
	{
		Mezzo = new MezzoDTO();
	}
	
	public MezzoDTO getMezzo()
	{
		return Mezzo;
	}
	
	public void setMezzo(MezzoDTO Mezzo)
	{
		this.Mezzo=Mezzo;
	}
	
	public int getOraP()
	{
		return OraP;
	}
	
	public void setOraP(int OraP)
	{
		this.OraP=OraP;
	}
	
	public int getOraA()
	{
		return OraA;
	}
	
	public void setOraA(int OraA)
	{
		this.OraA=OraA;
	}
	

	public void newMezzo()
	{

		if(!Mezzo.getLuogoArrivo().isEmpty() && !Mezzo.getLuogoPartenza().isEmpty() && Mezzo.getPrezzo()!=null){
			FacesContext context = FacesContext.getCurrentInstance();  
			
			Time OraAT;
			Time OraPT;
   
			OraAT = new Time (OraA, minA, 00);
			OraPT = new Time (OraP, minP, 00);
			Mezzo.setOraArrivo(OraAT);
			Mezzo.setOraPartenza(OraPT);
			System.out.println (Mezzo.getOraArrivo());
			ANM.add(Mezzo);
			context.addMessage(null, new FacesMessage("Mezzo inserito con successo!", "" ));
			this.Mezzo = null;
			OraP = 0;
			minP= 0;
			OraA=0;
			minA = 0;
			RequestContext context2 = RequestContext.getCurrentInstance();
			context2.update("form:PPGrid");
		}else{
			FacesContext context = FacesContext.getCurrentInstance(); 
			context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR ,"Attenzione!", "Non puoi lasciare i campi vuoti!" ));
		}
	}
	
	@SuppressWarnings("deprecation")
	public void prova()
	{
		System.out.println("bla entro");
//		Mezzo.setHA(OraA.getHours());
//		Mezzo.setmA(OraA.getMinutes());
//		Mezzo.setHP(OraP.getHours());
//		Mezzo.setmP(OraP.getMinutes());
		System.out.println("adesso sta con"+OraP);
	}


}
